﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookStore.Common;
using System.Data.SqlClient;
using BookStore.Models;
using System.Data;
namespace BookStore.Controllers
{
    public class PositionController : Controller
    {
        private static PositionController _controller = null;
        public static PositionController getController()
        {
            if (_controller == null)
                return new PositionController();
            else
                return _controller;
        }
        public PositionController()
        {
            _controller = this;
        }


        internal System.Data.DataTable getAllPosition()
        {
            con = BSCommon.getConnection();
            con.Open();
            cmd = new SqlCommand("Select * from Position", con);
            cmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable("Position");
            dt.Load(cmd.ExecuteReader());
            con.Dispose();
            con.Close();
            return dt;
        }

        internal DataTable getPositionID()
        {
            con = BSCommon.getConnection();
            con.Open();
            String sql = @"SELECT [PositionID]
                           FROM [BookStore].[dbo].[POSITION]";
            cmd = new SqlCommand(sql, con);
            cmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable("PositionID");
            dt.Load(cmd.ExecuteReader());
            con.Dispose();
            con.Close();
            return dt;
        }

        internal DataTable getPositionName()
        {
            con = BSCommon.getConnection();
            con.Open();
            String sql = @"SELECT [PositionName]
                           FROM [BookStore].[dbo].[POSITION]";
            cmd = new SqlCommand(sql, con);
            cmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable("PositionName");
            dt.Load(cmd.ExecuteReader());
            con.Dispose();
            con.Close();
            return dt;
        }

        internal String getPositionNameFromID(String PositionID)
        {
            con = BSCommon.getConnection();
            con.Open();
            String sql = @"SELECT [PositionName]
                           FROM [BookStore].[dbo].[POSITION]
                           WHERE [PositionID] = " + PositionID;
            cmd = new SqlCommand(sql, con);
            cmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable("PositionName");
            dt.Load(cmd.ExecuteReader());
            con.Dispose();
            con.Close();
            if (dt.Rows.Count > 0)
                return dt.Rows[0][0].ToString();
            else
                return "";
        }
    }
}
